using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using SumerCoreDevOps.Application.Nodes.Queries;
using SumerCoreDevOps.Infrastructure.Scheduling;
using SumerCoreDevOps.Shared.Models;

namespace SumerCoreDevOps.ControlApi.Controllers;

/// <summary>
/// 节点管理接口
/// </summary>
[ApiController]
[Route("api/[controller]")]
[Authorize]
public class NodeController : ControllerBase
{
    private readonly IMediator _mediator;
    private readonly ILogger<NodeController> _logger;

    public NodeController(IMediator mediator, ILogger<NodeController> logger)
    {
        _mediator = mediator;
        _logger = logger;
    }

    /// <summary>
    /// 获取所有在线节点
    /// </summary>
    /// <returns>返回当前所有在线执行节点的列表,每个节点包括节点ID、名称、IP地址、端口、状态、最后心跳时间等信息;数据封装在ApiResponse对象中</returns>
    [HttpGet("online")]
    [ProducesResponseType(typeof(ApiResponse<List<Domain.Entities.NodeStatus>>), 200)]
    public async Task<IActionResult> GetOnlineNodes()
    {
        var nodes = await _mediator.Send(new GetOnlineNodesQuery());
        return Ok(ApiResponse.Ok(nodes));
    }

    /// <summary>
    /// 获取节点统计信息
    /// </summary>
    /// <returns>返回节点统计数据对象,包括在线节点总数、离线节点总数、节点负载分布、任务执行统计等信息;数据封装在ApiResponse对象中</returns>
    [HttpGet("statistics")]
    [ProducesResponseType(typeof(ApiResponse<NodeStatistics>), 200)]
    public async Task<IActionResult> GetStatistics()
    {
        var stats = await _mediator.Send(new GetNodeStatisticsQuery());
        return Ok(ApiResponse.Ok(stats));
    }
}
